package edu.nf.takeoutdemo.web.admin;

import edu.nf.takeoutdemo.commons.base.BaseController;
import edu.nf.takeoutdemo.commons.properties.JwtProperties;
import edu.nf.takeoutdemo.commons.result.PageResult;
import edu.nf.takeoutdemo.commons.result.Result;
import edu.nf.takeoutdemo.commons.utils.JwtUtil;
import edu.nf.takeoutdemo.model.converter.EmployeeConverter;
import edu.nf.takeoutdemo.model.dto.EmployeeDTO;
import edu.nf.takeoutdemo.model.dto.EmployeeLoginDTO;
import edu.nf.takeoutdemo.model.dto.EmployeePageQueryDTO;
import edu.nf.takeoutdemo.model.entity.Employee;
import edu.nf.takeoutdemo.model.vo.EmployeeLoginVO;
import edu.nf.takeoutdemo.service.EmployeeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 *
 * @author zjl
 * @date 2025/10/23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController extends BaseController {
    private final EmployeeService employeeService;
    private final EmployeeConverter employeeConverter;
    private final JwtProperties  jwtProperties;

    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}",employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);

        Map<String,Object> claims = new HashMap<>();
        claims.put("empId",employee.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);
        EmployeeLoginVO employeeLoginVO = employeeConverter.toEmployeeLoginVO(employee);
        employeeLoginVO.setToken(token);
        return success(employeeLoginVO);
    }

    @PostMapping("/logout")
    public Result<String> logout(){
        return success();
    }

    @PostMapping
    public Result<?> save(@RequestBody EmployeeDTO  employeeDTO){
        log.info("新增员工：{}",employeeDTO);
        employeeService.save(employeeDTO);
        return success();
    }

    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询：参数为：{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return success(pageResult);
    }

    @PostMapping("/status/{status}")
    public Result<?> startOrStop(@PathVariable("status") Integer status,Long id){
        log.info("启用禁用员工账号：{},{}",status,id);
        employeeService.startOrStop(status,id);
        return success();
    }
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable("id") Long id){
        Employee employee =  employeeService.getById(id);
        return success(employee);
    }

    @PutMapping
    public Result<?> update(@RequestBody EmployeeDTO  employeeDTO){
        log.info("编辑员工信息：{}",employeeDTO);
        employeeService.update(employeeDTO);
        return success();
    }
}
